package com.cj.cms.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.cj.common.entity.IdEntity;
import com.cj.common.entity.Parent;
import com.cj.sys.entity.security.User;

@Entity
@Table(name = "CHANNEL")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region="Channel")
public class Channel extends IdEntity{
	public final static String NEED_LOGIN = "channel_login"; 
	public Channel(){
	}
	public Channel(Long id){
		super.setId(id);
	}
	private WebSite webSite;
	@Parent(textProperty="name")
	private Channel parent; 
	
	@Transient
	private List<Channel> sortChildren;
	@Transient
	private String navigation;
	private Set<Channel> children = new LinkedHashSet<Channel>(); // 有序的关联对象集合.
	private String code;
	private String name;
	private Integer orderNum;
    private Date createTime;
    private Date modifyTime;	
	private String fullName;
	private User createUser;
	private User modifyUser;
    /**
     * 栏目状态
    */
	private String status;
	/**
	 * 模板
	 */
	private Template template;
	/**
	 * 内容模板
	 */
	private Template contentTemplate;
	
	/**
	 * 附带模板
	 */
	private Template pertainTemplate;
	
	private String otherTemplateIds;
	
	/**
	 * 默认页面内容
	 */
	private String content;
	/**
	 * 生成路径
	 */
	private String  path;
	private String rssPath;
	
	/**
	 * 栏目logo
	 */
	private String titleImg;
    /**
     * 列表页信息条数
    */
	private Integer articleNum;
	
	//SEO优化
	private String keywords;
	private String description;
	private String title;
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "PARENT")
	public Channel getParent() {
		return parent;
	}
	public void setParent(Channel parent) {
		this.parent = parent;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getOrderNum() {
		return orderNum;
	}
	public void setOrderNum(Integer orderNum) {
		this.orderNum = orderNum;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Date getModifyTime() {
		return modifyTime;
	}
	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	public String getFullName() {
		return fullName;
	}
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "TEMPLATE")
	public Template getTemplate() {
		return template;
	}
	public void setTemplate(Template template) {
		this.template = template;
	}
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CONTENT_TEMPLATE")
	public Template getContentTemplate() {
		return contentTemplate;
	}
	public void setContentTemplate(Template contentTemplate) {
		this.contentTemplate = contentTemplate;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "PERTAIN_TEMPLATE")
	public Template getPertainTemplate() {
		return pertainTemplate;
	}
	public void setPertainTemplate(Template pertainTemplate) {
		this.pertainTemplate = pertainTemplate;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getTitleImg() {
		return titleImg;
	}
	public void setTitleImg(String titleImg) {
		this.titleImg = titleImg;
	}
	public Integer getArticleNum() {
		return articleNum;
	}
	public void setArticleNum(Integer articleNum) {
		this.articleNum = articleNum;
	}
	public String getKeywords() {
		return keywords;
	}
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	@OneToMany(mappedBy = "parent")
	public Set<Channel> getChildren() {
		return children;
	}
	
	@Transient
	public List<Channel> getSortChildren(){
		if(this.sortChildren != null)
			return sortChildren;
		if(this.getChildren().isEmpty())
			return null;
		sortChildren = new ArrayList<Channel>(this.getChildren());
		Collections.sort(sortChildren);
		return sortChildren;
	}
	
	public void setChildren(Set<Channel> children) {
		this.children = children;
	}
	public String getRssPath() {
		return rssPath;
	}
	public void setRssPath(String rssPath) {
		this.rssPath = rssPath;
	}
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "WEB_SITE")
	public WebSite getWebSite() {
		return webSite;
	}
	public void setWebSite(WebSite webSite) {
		this.webSite = webSite;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "CREATE_USER")
	public User getCreateUser() {
		return createUser;
	}
	public void setCreateUser(User createUser) {
		this.createUser = createUser;
	}
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "MODIFY_USER")
	public User getModifyUser() {
		return modifyUser;
	}
	public void setModifyUser(User modifyUser) {
		this.modifyUser = modifyUser;
	}
	
	@Transient
	public List<Long> getOtherTemplateIdsList(){
		if(StringUtils.isEmpty(this.otherTemplateIds))
			return  null;
		String[] templates = this.otherTemplateIds.split(",");
		List<Long> longIds = new ArrayList<Long>();
		for(String longId : templates){
			longIds.add(Long.valueOf(longId.trim()));
		}
		return longIds;
	}
	public String getOtherTemplateIds() {
		return otherTemplateIds;
	}
	public void setOtherTemplateIds(String otherTemplateIds) {
		this.otherTemplateIds = otherTemplateIds;
	}
	
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	@Transient
	public String getNavigation() {
		if(navigation == null){
			navigation =  buildeNavigation(this);
		}
		return navigation;
	}
	
	private static String buildeNavigation(Channel channel){
		StringBuilder builder = new StringBuilder();
		if(channel.getParent() != null && !channel.getParent().getCode().equalsIgnoreCase("root")){
			builder.append(buildeNavigation(channel.getParent())).append("-&gt;");
		}else if(channel.getParent() != null && channel.getParent().getCode().equalsIgnoreCase("root")){
			builder.append("<a href=\"")
			.append(channel.getWebSite().getSiteUrl()).append("/home/index.html")
				.append("\">网站首页</a>").append("-&gt;");
		}
		builder.append("<a href=\"")
		.append(channel.getWebSite().getSiteUrl()).append("/").append(channel.code).append("/index.html")
		.append("\">").append(channel.name).append("</a>");
		return builder.toString();
	}
	public static void main(String[] args) {
		Channel channel = new Channel(123L);
		channel.setParent(channel);
		System.out.println(channel.toString());
	}
}
